package de.dailab.prefgen;

import java.lang.reflect.Method;

import de.dailab.prefgen.INameMapping.TYPE;
import de.joecks.xtoo.DefaultMapper;

import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.Preference.OnPreferenceChangeListener;

/**
 * This Leaf Generator Interface generates non recursively Leafs of Objects
 * 
 * @author joecks
 * 
 */
public interface ILeafFactory {

	
	public boolean shouldGenerate(Class type);

	public Preference generate(Object superObject, Method getter,
			PreferenceActivity context, INameMapping nameMapping);

	public class OnLeafPrefernceChangedListener implements OnPreferenceChangeListener {

		private final Object superObject;
		private DefaultMapper defaultMapper;
		private final String parameterName;
		private final Method getter;
		private final INameMapping nameMapper;

		public OnLeafPrefernceChangedListener(Object superObject, Method getter, INameMapping nameMapper) {
			this.superObject = superObject;
			this.getter = getter;
			this.nameMapper = nameMapper;
			this.parameterName = Utils.stripPrefix(getter.getName());
		}
		
		@Override
		public boolean onPreferenceChange(Preference preference, Object newValue) {
			defaultMapper = new DefaultMapper();
			defaultMapper.setValue(newValue, superObject,parameterName, "");
			System.out.println(preference.getKey() + " was changed to " + newValue );
			preference.setSummary(nameMapper.toHumanReadable(getter, superObject, TYPE.DESCRIPTION));
			return true;
		}
		
	}
	
}


